package cn.iocoder.yudao.module.happycore.controller.admin.oxdirectory;

import cn.iocoder.yudao.module.happycore.controller.admin.oxdirectory.vo.OxDirectoryPageReqVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxdirectory.vo.OxDirectoryRespVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxdirectory.vo.OxDirectorySaveReqVO;
import cn.iocoder.yudao.module.happycore.dal.dataobject.oxdirectory.OxDirectoryDO;
import cn.iocoder.yudao.module.happycore.service.oxdirectory.OxDirectoryService;
import cn.iocoder.yudao.module.happycore.service.oxproject.OxProjectService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 目录")
@RestController
@RequestMapping("/happy-ox-core/ox-directory")
@Validated
public class OxDirectoryController {

    @Resource
    private OxDirectoryService oxDirectoryService;
    @Resource
    private OxProjectService oxProjectService;

    @PostMapping("/create")
    @Operation(summary = "创建目录")
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-directory:create')")
    public CommonResult<Long> createOxDirectory(@Valid @RequestBody OxDirectorySaveReqVO createReqVO) {
        return success(oxDirectoryService.createOxDirectory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新目录")
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-directory:update')")
    public CommonResult<Boolean> updateOxDirectory(@Valid @RequestBody OxDirectorySaveReqVO updateReqVO) {
        oxDirectoryService.updateOxDirectory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除目录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-directory:delete')")
    public CommonResult<Boolean> deleteOxDirectory(@RequestParam("id") Long id) {
        oxDirectoryService.deleteOxDirectory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除目录")
                @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-directory:delete')")
    public CommonResult<Boolean> deleteOxDirectoryList(@RequestParam("ids") List<Long> ids) {
        oxDirectoryService.deleteOxDirectoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得目录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-directory:query')")
    public CommonResult<OxDirectoryRespVO> getOxDirectory(@RequestParam("id") Long id) {
        OxDirectoryDO oxDirectory = oxDirectoryService.getOxDirectory(id);
        return success(BeanUtils.toBean(oxDirectory, OxDirectoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得目录分页")
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-directory:query')")
    public CommonResult<PageResult<OxDirectoryRespVO>> getOxDirectoryPage(@Valid OxDirectoryPageReqVO pageReqVO) {
        PageResult<OxDirectoryDO> pageResult = oxDirectoryService.getOxDirectoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OxDirectoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出目录 Excel")
    @PreAuthorize("@ss.hasPermission('happy-ox-core:ox-directory:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOxDirectoryExcel(@Valid OxDirectoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OxDirectoryDO> list = oxDirectoryService.getOxDirectoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "目录.xls", "数据", OxDirectoryRespVO.class,
                        BeanUtils.toBean(list, OxDirectoryRespVO.class));
    }

    @PostMapping("/getDirectoryByProjectId")
    public CommonResult getDirectoryByProjectId(@RequestParam("projectId") Long projectId){
        List<OxDirectoryRespVO> oxDirectoryRespVOS = oxDirectoryService.getDirectoryByProjectId(projectId);
        return success(oxDirectoryRespVOS);
    }

    @PostMapping("/addType")
    public CommonResult addDirectory(@RequestParam("projectId")Long projectId,@RequestParam("name") String name){
        oxProjectService.addType(projectId,name);
        return success(true);
    }
    @PostMapping("/getDirectoryPath")
    public CommonResult getDirectoryPath(@RequestParam("id") Long id){
        String path = oxProjectService.getDirectoryPath(id);
        return success(path);
    }
}